package com.example.csetpp.controller;

import com.example.csetpp.constants.JWTConstant;
import com.example.csetpp.constants.WeChatConstant;
import com.example.csetpp.pojo.dto.PageUserDTO;
import com.example.csetpp.pojo.dto.RecordId;
import com.example.csetpp.pojo.dto.problem.ProblemDTO;
import com.example.csetpp.pojo.dto.user.*;
import com.example.csetpp.pojo.entity.Problem;
import com.example.csetpp.pojo.entity.User;
import com.example.csetpp.service.UserService;
import com.example.csetpp.utils.JwtUtil;
import com.example.csetpp.utils.Result;
import com.example.csetpp.pojo.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@CrossOrigin
@RestController
@Tag(name = "用户功能相关")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "账户邮箱登录")
    @PostMapping("/login")
    public Result<String> userLogin(@RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(WeChatConstant.USER_ID, userLoginVO.getId());
        String token = JwtUtil.createJWT(JWTConstant.SECRET_KEY, JWTConstant.JWT_TTL, claims);
        userLoginVO.setToken(token);
        return Result.success(token);
    }

    @Operation(summary = "获取用户信息")
    @PostMapping("/profile")
    public Result<UserProfile> getProfile() {
        UserProfile userVO = userService.getProfile();
        return Result.success(userVO);
    }


//    @Operation(summary = "自动登录功能")
//    @PostMapping("/auto_login_user")
//    public Result<UserLoginVO> autoLoginUser(@RequestBody TokenDTO tokenDTO) {
//        UserLoginVO userLoginVO = userService.autoLogin(tokenDTO);
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(WeChatConstant.USER_ID,userLoginVO.getId());
//        String token = JwtUtil.createJWT(JWTConstant.SECRET_KEY, JWTConstant.JWT_TTL, claims);
//        userLoginVO.setToken(token);
//        return Result.success(userLoginVO);
//    }



//    @Operation(summary = "手机号获取")
//    @PostMapping("/loginWithLocalPhone")
//    public Result<UserLoginVO> userLoginWithLocalPhone(@RequestBody UserLoginWithLocalPhone userLoginWithLocalPhone)
//    {
//        UserLoginVO userLoginVO =  userService.loginWithLocalPhone(userLoginWithLocalPhone);
//        return Result.success(userLoginVO);
//    }

    @Operation(summary = "发送邮件验证码功能")
    @PostMapping("/senMailCode")
    public Result<?> senMailCode(@RequestBody SendCodeDTO mail) {
        userService.senMailCode(mail.getMail());
        return Result.success("OK");
    }

    @Operation(summary = "验证验证码功能")
    @PostMapping("/verifyCode")
    public Result<?> verifyCode(@RequestBody VerifyCodeDTO verifyCodeDTO) {
        return Result.success(userService.verifyCode(verifyCodeDTO.getCode(), verifyCodeDTO.getEmail()));
    }

    @Operation(summary = "用户注册功能")
    @PostMapping("/register")
    public Result<UserLoginVO> userRegister(@RequestBody User user) {
        UserLoginVO userLoginVO = userService.register(user);
        return Result.success(userLoginVO);
    }

    @Operation(summary = "用户信息修改")
    @PostMapping("/edit")
    public Result<?> userEdit(@RequestBody UserEditDTO userLoginDTO) {
        String userEditVO = userService.update(userLoginDTO);
        return Result.success(userEditVO);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/change-password")
    public Result<?> changePassword(@RequestBody UserChangePasswordDTO userChangePasswordDTO) {
        String result = userService.changePassword(userChangePasswordDTO);
        return Result.success(result);
    }

    @Operation(summary = "注销用户功能")
    @PostMapping("/delete")
    public Result<?> userDelete(@RequestBody Map<String, String> payload){
        String id = payload.get("id");
        return Result.success(userService.delete(id));
    }

    @Operation(summary = "分页获取用户列表，无条件")
    @PostMapping("/get-page-user-list")
    public Result<?> getPageUserList(@RequestBody PageUserDTO pageUserDTO){
        return Result.success(userService.getPageUserList(pageUserDTO));
    }

    @Operation(summary = "分页获取用户列表")
    @PostMapping("/search-user")
    public Result<?> searchUser(@RequestBody UserSearchDTO userSearchDTO){
        return Result.success(userService.searchUser(userSearchDTO));
    }

//    @Operation(summary = "用户提交问题反馈")
//    @PostMapping("/question-feedback")
//    public Result<?> questionFeedback(@RequestBody Problem problem){
//        userService.questionFeedback(problem);
//        return Result.success("OK");
//    }
//
//    @Operation(summary = "后台分页获取问题反馈")
//    @PostMapping("/get-question-feedback")
//    public Result<?> getQuestionFeedback(@RequestBody PageUserDTO pageUserDTO){
//        return Result.success(userService.getQuestionFeedback(pageUserDTO));
//    }
//
//    @Operation(summary = "根据id删除反馈记录")
//    @PostMapping("/delete-question-feedback")
//    public Result<?> deleteQuestionFeedback(@RequestBody RecordId recordId){
//        userService.deleteQuestionFeedback(recordId);
//        return Result.success("OK");
//    }



}
